/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hr.fer.zpr.nmbp.project3.dao.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

/**
 *
 * @author Miki
 */
/**
 * Pomoćni razred sa statičkim metodama
 * za dohvat teksta iz class-patha.
 */
public class CPLoader {
	
	public static Properties loadProperties(String path) {
		InputStream is = null;
		Reader r = null;
		try {
			is = CPLoader.class.getClassLoader().getResourceAsStream(path);
			r = new InputStreamReader(is, "UTF-8");
			Properties p = new Properties();	
			p.load(r);
			return p;
		} catch (Exception e) {
			return null;
		} finally {
			try { is.close(); } catch (Exception ignorable) {}
			try { r.close(); } catch (Exception ignorable) {}
		}
	}
	
	public static String loadString(String path) {
		InputStream is = null;
		BufferedReader br = null;
		try {
			is = CPLoader.class.getClassLoader().getResourceAsStream(path);
			br = new BufferedReader( 
					new InputStreamReader(is, "UTF-8"));
			
			StringBuilder sb = new StringBuilder();
			char[] buffer = new char[1024];
			int length;
			
			while ((length = br.read(buffer)) > 0) {
				sb.append(buffer, 0, length);
			}
			return sb.toString();
			
		} catch (Exception e) {
			return null;
		} finally {
			try { is.close(); } catch (Exception ignorable) {}
			try { br.close(); } catch (Exception ignorable) {}
		}
	}
}
